using OrderSystem.SystemFramework;
using System;
using System.Data;
using System.Data.SqlClient;

namespace OrderSystem.DataAccess
{
	public class ProductTypes : IDisposable
	{
		private SqlDataAdapter dataAdapter;

		private SqlCommand selectCommand;

		private SqlCommand insertCommand;

		private SqlCommand updateCommand;

		private SqlCommand deleteCommand;

		public DataTable GetProductTypes(string strQuery)
		{
			DataTable dataTable = new DataTable("ProductType");
			try
			{
				this.selectCommand = new SqlCommand(strQuery, new SqlConnection(AppSettings.ConnectionString));
				this.selectCommand.Connection.Open();
				this.dataAdapter = new SqlDataAdapter(this.selectCommand);
				this.dataAdapter.Fill(dataTable);
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return dataTable;
		}

		public DataTable LoadProductType(int productTypeID)
		{
			DataTable dataTable = new DataTable("ProductType");
			try
			{
				string cmdText = "SELECT PKID AS 序号,[Name] AS 类型名称,Thick AS 厚度规格,Unit AS 单位,DefaultPrice as 默认价格,LabelMultiple as 标签倍数,Remark AS 备注,LimitedQuantity as 流程卡数量上限,LimitedArea as 流程卡面积上限 FROM ProductType WHERE PKID=@ProductTypeID";
				this.selectCommand = new SqlCommand(cmdText, new SqlConnection(AppSettings.ConnectionString));
				this.selectCommand.Connection.Open();
				this.selectCommand.Parameters.Add(new SqlParameter("@ProductTypeID", SqlDbType.Int));
				this.selectCommand.Parameters["@ProductTypeID"].Value = productTypeID;
				this.dataAdapter = new SqlDataAdapter(this.selectCommand);
				this.dataAdapter.Fill(dataTable);
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return dataTable;
		}

		public bool InsertProductType(string productName, string thick, string unit, decimal mDefaultPrice, int nLabelMultiple, string remark, int nLimitedQuantity, decimal fLimitedArea)
		{
			try
			{
				this.insertCommand = new SqlCommand("insert into ProductType select @ProductName,@Thick,@Unit,@DefaultPrice,@LabelMultiple,@Remark,case when max([Sequence])+1 is null then 0 else max([Sequence])+1 end,@LimitedQuantity,@LimitedArea from ProductType", new SqlConnection(AppSettings.ConnectionString));
				this.insertCommand.Connection.Open();
				this.insertCommand.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.NVarChar, 128));
				this.insertCommand.Parameters["@ProductName"].Value = productName;
				this.insertCommand.Parameters.Add(new SqlParameter("@Thick", SqlDbType.NVarChar, 16));
				this.insertCommand.Parameters["@Thick"].Value = thick;
				this.insertCommand.Parameters.Add(new SqlParameter("@Unit", SqlDbType.NVarChar, 8));
				this.insertCommand.Parameters["@Unit"].Value = unit;
				this.insertCommand.Parameters.Add(new SqlParameter("@DefaultPrice", SqlDbType.Money));
				this.insertCommand.Parameters["@DefaultPrice"].Value = mDefaultPrice;
				this.insertCommand.Parameters.Add(new SqlParameter("@LabelMultiple", SqlDbType.Int));
				this.insertCommand.Parameters["@LabelMultiple"].Value = nLabelMultiple;
				this.insertCommand.Parameters.Add(new SqlParameter("@Remark", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters["@Remark"].Value = remark;
				this.insertCommand.Parameters.Add(new SqlParameter("@LimitedQuantity", SqlDbType.Int));
				this.insertCommand.Parameters["@LimitedQuantity"].Value = nLimitedQuantity;
				this.insertCommand.Parameters.Add(new SqlParameter("@LimitedArea", SqlDbType.Decimal));
				this.insertCommand.Parameters["@LimitedArea"].Value = fLimitedArea;
				this.insertCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public bool UpdateProductType(int productTypeID, string productName, string thick, string unit, decimal mDefaultPrice, int nLabelMultiple, string remark, int nLimitedQuantity, decimal fLimitedArea)
		{
			try
			{
				this.updateCommand = new SqlCommand("UPDATE ProductType SET [Name]=@ProductName,Thick=@Thick,Unit=@Unit,DefaultPrice=@DefaultPrice,LabelMultiple=@LabelMultiple,Remark=@Remark,LimitedQuantity=@LimitedQuantity,LimitedArea=@LimitedArea WHERE PKID=@ProductTypeID", new SqlConnection(AppSettings.ConnectionString));
				this.updateCommand.Connection.Open();
				this.updateCommand.Parameters.Add(new SqlParameter("@ProductTypeID", SqlDbType.Int));
				this.updateCommand.Parameters["@ProductTypeID"].Value = productTypeID;
				this.updateCommand.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.NVarChar, 128));
				this.updateCommand.Parameters["@ProductName"].Value = productName;
				this.updateCommand.Parameters.Add(new SqlParameter("@Thick", SqlDbType.NVarChar, 16));
				this.updateCommand.Parameters["@Thick"].Value = thick;
				this.updateCommand.Parameters.Add(new SqlParameter("@Unit", SqlDbType.NVarChar, 8));
				this.updateCommand.Parameters["@Unit"].Value = unit;
				this.updateCommand.Parameters.Add(new SqlParameter("@DefaultPrice", SqlDbType.Money));
				this.updateCommand.Parameters["@DefaultPrice"].Value = mDefaultPrice;
				this.updateCommand.Parameters.Add(new SqlParameter("@LabelMultiple", SqlDbType.Int));
				this.updateCommand.Parameters["@LabelMultiple"].Value = nLabelMultiple;
				this.updateCommand.Parameters.Add(new SqlParameter("@Remark", SqlDbType.NVarChar, 64));
				this.updateCommand.Parameters["@Remark"].Value = remark;
				this.updateCommand.Parameters.Add(new SqlParameter("@LimitedQuantity", SqlDbType.Int));
				this.updateCommand.Parameters["@LimitedQuantity"].Value = nLimitedQuantity;
				this.updateCommand.Parameters.Add(new SqlParameter("@LimitedArea", SqlDbType.Decimal));
				this.updateCommand.Parameters["@LimitedArea"].Value = fLimitedArea;
				this.updateCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(true);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (this.dataAdapter != null)
				{
					if (this.dataAdapter.SelectCommand != null)
					{
						if (this.dataAdapter.SelectCommand.Connection != null)
						{
							this.dataAdapter.SelectCommand.Connection.Dispose();
							this.dataAdapter.SelectCommand.Connection = null;
						}
						this.dataAdapter.SelectCommand.Dispose();
						this.dataAdapter.SelectCommand = null;
					}
					this.dataAdapter.Dispose();
					this.dataAdapter = null;
				}
				if (this.insertCommand != null)
				{
					if (this.insertCommand.Connection != null)
					{
						this.insertCommand.Connection.Dispose();
						this.insertCommand.Connection = null;
					}
					this.insertCommand.Dispose();
					this.insertCommand = null;
				}
				if (this.updateCommand != null)
				{
					if (this.updateCommand.Connection != null)
					{
						this.updateCommand.Connection.Dispose();
						this.updateCommand.Connection = null;
					}
					this.updateCommand.Dispose();
					this.updateCommand = null;
				}
				if (this.deleteCommand != null)
				{
					if (this.deleteCommand.Connection != null)
					{
						this.deleteCommand.Connection.Dispose();
						this.deleteCommand.Connection = null;
					}
					this.deleteCommand.Dispose();
					this.deleteCommand = null;
				}
			}
		}
	}
}
